package com.ruoyi.carShop.controller;



import com.ruoyi.carShop.domain.Activity;
import com.ruoyi.carShop.mapper.ActivityMapper;

import com.ruoyi.carShop.service.LoadService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
@CrossOrigin
@RestController
@RequestMapping("/carShop")
public class ActivityController {

    @Autowired
    private LoadService loadService;
    @Autowired
    private ActivityMapper activityMapper;
    @RequestMapping("/activity/list")
    public List<Activity> queryActivity(Activity activity){
        RequestAttributes attributes = RequestContextHolder.getRequestAttributes();
        ServletRequestAttributes sra = (ServletRequestAttributes)attributes;
        HttpServletRequest request = sra.getRequest();
        //2 从请求头获取token
        String token = request.getHeader("token");
        int mId = loadService.getToken(token);
        activity.setRiShopId((long) mId);
        return activityMapper.queryActivity(activity);
    }
    @RequestMapping("/activity/listWei")
    public List<Activity> queryActivityWei(Activity activity) {
        RequestAttributes attributes = RequestContextHolder.getRequestAttributes();
        ServletRequestAttributes sra = (ServletRequestAttributes)attributes;
        HttpServletRequest request = sra.getRequest();
        //2 从请求头获取token
        String token = request.getHeader("token");
        int mId = loadService.getToken(token);
        activity.setRiShopId((long) mId);
        return activityMapper.queryActivityWei(activity);
    }
    @RequestMapping("/activity/add")
    public int addActivity(@RequestBody Activity activity) {
        RequestAttributes attributes = RequestContextHolder.getRequestAttributes();
        ServletRequestAttributes sra = (ServletRequestAttributes)attributes;
        HttpServletRequest request = sra.getRequest();
        //2 从请求头获取token
        String token = request.getHeader("token");
        int mId = loadService.getToken(token);
        activity.setRiShopId((long) mId);
        return activityMapper.addActivity(activity);
    }

    @RequestMapping("/activity/updatestate")
    public int updateActivityState(Integer id) {
        return activityMapper.updateActivityState(id);
    }
    @RequestMapping("/activity/updatestate2")
    public int updateActivityState2(Integer id) {
        return activityMapper.updateActivityState2(id);
    }
    @RequestMapping("/activity/queryActivityById")
    public Activity queryActivityById(Integer id) {
        return activityMapper.queryActivityById(id);
    }
    @RequestMapping("/activity/deleteActivityById")
    public int deleteActivityById(Integer id) {
        return activityMapper.deleteActivityById(id);
    }
}
